package com.qinhailin.portal.dbm.managectrl;

import java.sql.Timestamp;
import java.util.Random;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import com.qinhailin.common.base.BaseController;
import com.qinhailin.common.routes.ControllerBind;
import com.qinhailin.common.util.DateUtil;
import com.qinhailin.common.vo.Feedback;
import com.qinhailin.portal.dbm.model.CTCustomer;
import com.qinhailin.portal.dbm.model.DTCustomerContact;
import com.qinhailin.portal.dbm.model.DTSchedule;
import com.qinhailin.portal.dbm.service.CPGetIDService;
import com.qinhailin.portal.dbm.service.CTCustomerServiceImpl;
import com.qinhailin.portal.dbm.service.DTContactServiceImpl;
import com.qinhailin.portal.dbm.service.DTCustomerContactServiceImpl;
import com.qinhailin.portal.dbm.service.DTQueryServiceImpl;
import com.qinhailin.portal.dbm.service.DTScheduleServiceImpl;
import com.qinhailin.portal.dbm.service.DTSystemdataServiceImpl;


@ControllerBind(path="/portal/dbm/customer")
public class CustomerController extends BaseController{
	
	@Inject
	CTCustomerServiceImpl service;
	@Inject
	CPGetIDService idService;
	@Inject
	DTSystemdataServiceImpl systemdataService;
	
	
	public void index() {
		this.render("index.html");
	}
	
	public void list() {
		//Record record = new Record();
		//this.renderJson(this.service.page(getParaToInt("Record record = new Record();pageNumber", 1), getParaToInt("pageSize", 10), record));
		Record record = new Record();
		record.set("xName", getPara("xName"));
		renderJson(service.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10), record));
	}
	
	public void delete() {
		service.deleteByIds(getIds());
		renderJson(Feedback.success());
	}
	
	private String genKey() {
		StringBuilder sb = new StringBuilder();
		Integer length = 24;	// 默认长度 10
		Random rand = new Random(DateUtil.getCurrentTimestamp().getTime());
		Integer i = 0;
		while(i < length) {
			sb.append((char)(rand.nextInt(24) + 65));
			i++;
		}
		return sb.toString();
	}
	
	public void add() {
		CTCustomer customer = new CTCustomer();
		customer
				.setXKey(this.genKey())
				.setXHeartbeatInterval(systemdataService.findByID(1001).getXInt() / 1000)
				.setXHeartbeatOuttime(systemdataService.findByID(1002).getXInt() / 1000)
				.setXIsInterval4(1).setXIsInterval5(1).setXIsInterval6(1)
				.setXIsInterval7(1).setXIsInterval8(1).setXIsInterval9(1);
		this.set("customer", customer);
		//this.set("customer.xKey", this.genKey());
		//DTSystemdata d = systemdataService.findByID(1001);
		//System.out.println(d);
		
		//this.set("customer.xHeartbeatInterval", systemdataService.findByID(1001).getXInt());
		//this.set("customer.HeartbeatOuttime", systemdataService.findByID(1002).getXInt());
		this.render("add.html");
	}
	
	public void save() {
		//CTCustomer customer = this.getModel(CTCustomer.class);
		CTCustomer customer = this.getBean(CTCustomer.class);
		customer.setXHeartbeatInterval(customer.getXHeartbeatInterval() * 1000)
				.setXHeartbeatOuttime(customer.getXHeartbeatOuttime() * 1000);
		
		if(!service.save(customer)) {
			setException("保存失败");
		}
		render("add.html");
	}
	
	
	public void update() {
		CTCustomer customer = this.getBean(CTCustomer.class);
		customer.setXHeartbeatInterval(customer.getXHeartbeatInterval() * 1000)
				.setXHeartbeatOuttime(customer.getXHeartbeatOuttime() * 1000)
				.setSLastUpdate(DateUtil.getCurrentTimestamp())
				.setSVersion(customer.getSVersion() != null ? customer.getSVersion() + 1 : 1)
				;
		
		if(!service.update(customer)) {
			setException("保存失败");
		}
		render("edit.html");
	}
	
	public void edit() {
		CTCustomer customer = service.findByID(this.getInt());
		customer.setXHeartbeatInterval(customer.getXHeartbeatInterval() / 1000).setXHeartbeatOuttime(customer.getXHeartbeatOuttime() / 1000);
		setAttr("customer", customer);
		render("edit.html");
	}
	
	@Inject
	DTScheduleServiceImpl scheduleService;
	@Inject
	DTQueryServiceImpl queryService;
	
	public void schedule() {
		Integer rCustID = this.getInt();
		this.schedule(rCustID);
	}
	
	private void schedule(Integer rCustID) {
		this.set("schedules", scheduleService.list(rCustID));
		this.set("querys", queryService.list());
		this.set("rCustID", rCustID);
		this.render("schedule.html");
	}
	
	public void updateSchedule() {
		Integer rCustID = this.getInt("rCustID", 0);
		String[] tasks = this.getParaValues("tasks");
		Timestamp now = DateUtil.getCurrentTimestamp();
		
		// 先清空之前的
		scheduleService.delete(rCustID, 1);
		
		if(tasks != null && tasks.length > 0) {
			for(String t : tasks) {
				
				Integer rQueryID = Integer.valueOf(t);
				
				DTSchedule s = new DTSchedule().setRCustID(rCustID).setRQueryID(rQueryID).setXDateTime(now).setXStatus(1).setXType(1);
				scheduleService.save(s);
			}
		}
		//this.render("schedule.html");
		this.schedule(rCustID);
	}
	
	@Inject
	DTContactServiceImpl contactService;
	@Inject
	DTCustomerContactServiceImpl customerContactService;
	
	private void contact(Integer rCustID) {
		this.set("rCustID", rCustID);
		//this.set(attributeName, attributeValue)
		this.set("contacts", contactService.list());
		this.set("customerContacts", customerContactService.list(rCustID));
		this.render("contact.html");
	}
	public void contact() {
		this.contact(this.getInt());
	}
	
	public void updateContact() {
		Integer rCustID = this.getInt("rCustID");
		String[] cs = this.getParaValues("contacts");
		customerContactService.delete(rCustID);
		if(cs != null) {
			for(String s : cs) {
				DTCustomerContact c = new DTCustomerContact().setMID(rCustID).setRContID(Integer.valueOf(s)).setXType(1);
				customerContactService.save(c);
			}
		}
		this.contact(rCustID);
	}

}
